package com.liuchuanzheng.lcz_wanandroid.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.databinding.FragmentQuestionBinding
import com.liuchuanzheng.lcz_wanandroid.net.Article
import com.liuchuanzheng.lcz_wanandroid.ui.adapter.HomeAdapter
import com.liuchuanzheng.lcz_wanandroid.ui.viewmodel.QuestionViewModel


class QuestionFragment : Fragment() {
    lateinit var viewModel:QuestionViewModel
    lateinit var mViewbinding: FragmentQuestionBinding
    val questionAdapter:HomeAdapter by lazy { HomeAdapter() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewbinding = FragmentQuestionBinding.inflate(inflater, container, false)
        return mViewbinding.root
    }

    companion object {
        @JvmStatic
        fun newInstance() = QuestionFragment()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(this).get(QuestionViewModel::class.java)
        viewModel.questionData.observe(requireActivity(), Observer {
            it.datas.let { article ->
                questionAdapter.run {
                    setList(article)
                }
            }
        })
        mViewbinding.recyclerView1Question.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = questionAdapter
        }
        net_getQuestionList(0)


    }
    private fun net_getQuestionList(page:Int){
        viewModel.getQustionList(page)
    }
}